
package br.com.aeternu.controleatendimento.manutencao.empresa.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import br.com.aeternu.controleatendimento.common.ui.EasyJDialog;

public class UIFormEmpresa extends EasyJDialog
{

   private static final long serialVersionUID = 1L;
   private final JPanel contentPanel = new JPanel();
   private JTextField empresa;
   private IfcUIFormEmpresa listener;
   private JComboBox<String> tipo;
   private JLabel lblEmpresa;
   private JLabel lblTipo;

   public UIFormEmpresa(IfcUIFormEmpresa listener)
   {
      super("Detalhes da Empresa");
      this.listener = listener;
      initComponents();
   }

   public JTextField getEmpresa()
   {
      if (empresa == null)
      {
         empresa = new JTextField();
         empresa.setColumns(10);
         empresa.getDocument().addDocumentListener(new DocumentListener()
         {

            @Override
            public void insertUpdate(DocumentEvent e)
            {
               handleEmpresaDocumentEvent(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e)
            {
               handleEmpresaDocumentEvent(e);
            }

            @Override
            public void changedUpdate(DocumentEvent e)
            {
               handleEmpresaDocumentEvent(e);
            }
         });
      }
      return empresa;
   }

   protected void handleEmpresaDocumentEvent(DocumentEvent e)
   {
      setMandatory(getLblEmpresa(), !validaEmpresa());
   }

   public JComboBox<String> getTipo()
   {
      if (tipo == null)
      {
         tipo = new JComboBox<String>();
         tipo.setEditable(true);
         AutoCompleteDecorator.decorate(tipo);
         tipo.addItemListener(new ItemListener()
         {

            public void itemStateChanged(final ItemEvent e)
            {
               handleTipoItemStateChanged(e);
            }
         });
      }
      return tipo;
   }

   protected void handleTipoItemStateChanged(ItemEvent e)
   {
      if (e.getStateChange() == ItemEvent.SELECTED)
      {
         boolean ret = validaTipo();

         setMandatory(getLblTipo(), !ret);
      }
   }

   private JLabel getLblEmpresa()
   {
      if (lblEmpresa == null)
      {
         lblEmpresa = new JLabel("Empresa:");
         lblEmpresa.setDisplayedMnemonic('E');
         lblEmpresa.setLabelFor(getEmpresa());
      }

      return lblEmpresa;
   }

   private JLabel getLblTipo()
   {
      if (lblTipo == null)
      {
         lblTipo = new JLabel("Tipo:");
         lblTipo.setDisplayedMnemonic('T');
         lblTipo.setLabelFor(getTipo());
      }

      return lblTipo;
   }

   @Override
   public void sair()
   {
      dispose();
   }

   private void initComponents()
   {
      setResizable(false);
      setBounds(100, 100, 320, 200);
      getContentPane().setLayout(new BorderLayout());
      contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
      getContentPane().add(contentPanel, BorderLayout.CENTER);
      GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
      gl_contentPanel.setHorizontalGroup(
               gl_contentPanel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPanel.createSequentialGroup()
                                 .addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
                                          .addComponent(getLblEmpresa())
                                          .addComponent(getEmpresa(), GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                                          .addComponent(getLblTipo())
                                          .addComponent(getTipo(), 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                 .addContainerGap(145, Short.MAX_VALUE))
               );
      gl_contentPanel.setVerticalGroup(
               gl_contentPanel.createParallelGroup(Alignment.LEADING)
                        .addGroup(
                           gl_contentPanel
                                    .createSequentialGroup()
                                    .addComponent(getLblEmpresa())
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(getEmpresa(), GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                       GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(getLblTipo())
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(getTipo(), GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                       GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap(144, Short.MAX_VALUE))
               );
      contentPanel.setLayout(gl_contentPanel);
      {
         JPanel buttonPane = new JPanel();
         buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
         getContentPane().add(buttonPane, BorderLayout.SOUTH);
         {
            JButton confirmar = new JButton("Confirmar");
            confirmar.setMnemonic('C');
            confirmar.addActionListener(new ActionListener()
            {

               public void actionPerformed(ActionEvent e)
               {
                  if (preenchimentoValido())
                     listener.confirmar();
               }
            });
            confirmar.setActionCommand("Confirmar");
            buttonPane.add(confirmar);
            getRootPane().setDefaultButton(confirmar);
         }
      }

      setVisible(true);
   }

   private void setMandatory(JLabel label, boolean mandatory)
   {
      if (mandatory)
      {
         label.setForeground(Color.RED);
         label.setFont(label.getFont().deriveFont(Font.ITALIC));
      }
      else
      {
         label.setForeground(SystemColor.textText);
         label.setFont(label.getFont().deriveFont(Font.PLAIN));
      }
   }

   private boolean preenchimentoValido()
   {
      boolean ret = true;

      ret = validaEmpresa() & validaTipo();

      return ret;
   }

   private boolean validaEmpresa()
   {
      boolean ret = true;

      if (getEmpresa().getText().isEmpty())
         ret = false;

      setMandatory(getLblEmpresa(), !ret);

      return ret;
   }

   private boolean validaTipo()
   {
      boolean ret = true;

      if (((String) getTipo().getSelectedItem()).isEmpty())
         ret = false;

      setMandatory(getLblTipo(), !ret);

      return ret;
   }
}
